import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreAppletSiteVersionService extends BaseService<any> {
  private readonly logger = new Logger(CoreAppletSiteVersionService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getPage
   * 对应 PHP: CoreAppletSiteVersionService_core::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(site_id: any[], where: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: if ($site_id > 0) {             $where[] = ['site_id', '=', $site_id];...
if (site_id > 0) {
            where[] = ['site_id', '=', site_id];
  }

  /**
   * getInfo
   * 对应 PHP: CoreAppletSiteVersionService_core::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(site_id: number, id: number) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: $field = 'id, site_id, version_id, type, action, version, version_num, create_time';         return $this->model->where([['id', '=', $id], ['site_id',...
const field = 'id, site_id, version_id, type, action, version, version_num, create_time';
        return this.model.where([['id', '=', id], ['site_id', '=', site_id))).field(field).with(.appletVersion).findOrEmpty().toArray(];
    }

    /**
     * 添加版本升级记录
     * @param int site_id
     * @param int version_id
     * @param string action
     * @return true
     */
    async add(int site_id, int version_id, string action)
    {
        const version_info = this.coreAppletVersionService.getInfo(version_id);
        if (!version_info)) throw new BusinessException('APPLET_VERSION_NOT_EXISTS'];
        data.site_id = site_id;
        data.type = version_info.type;
        data.create_time = time(];
        data.version_id = version_info.id;
        data.action = action;//操作方式
        this.model.create(data);
        return true;

    }


    /**
     * 获取最后一个下载或升级的版本
     * @param int site_id
     * @param string type
     * @param string action
     * @return mixed|string
     * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async getLastVersion(int site_id, string type, string action = '')
    {
        const where = [['site_id', '=', site_id], ['type', '=', type]];
        const list = this.model.where(where).with(.appletVersion).select().toArray(];
        const list = array_column(list, null, 'version_num'];
        ksort(list];
        const site_version = reset(list);
        return site_version.version || '';
    }

    /**
     * 获取当前站点最新可升级的小程序版本
     * @param int site_id
     * @param string type
     * @return void
     */
    async getUpgradeVersion(int site_id, string type)
    {
        //查询下一次升级或下载的版本
        const version = this.getLastVersion(site_id, type];
        const where = [['type', '=', type]);
        if (!version) {
            const version_num = version_to_int(version];
            where[] = ['version_num', '>', version_num);
  }

  /**
   * add
   * 对应 PHP: CoreAppletSiteVersionService_core::add()
   * 逻辑类型: undefined - undefined
   */
  async add(site_id: number, version_id: number, action: number) {
    // 基于PHP真实逻辑: add
    // PHP原文: $version_info = (new CoreAppletVersionService())->getInfo($version_id);         if (empty($version_info)) throw new CommonException('APPLET_VERSION_NO...
const version_info = this.coreAppletVersionService.getInfo(version_id);
        if (!version_info)) throw new BusinessException('APPLET_VERSION_NOT_EXISTS'];
        data.site_id = site_id;
        data.type = version_info.type;
        data.create_time = time(];
        data.version_id = version_info.id;
        data.action = action;//操作方式
        this.model.create(data);
        return true;

    }


    /**
     * 获取最后一个下载或升级的版本
     * @param int site_id
     * @param string type
     * @param string action
     * @return mixed|string
     * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async getLastVersion(int site_id, string type, string action = '')
    {
        const where = [['site_id', '=', site_id], ['type', '=', type]];
        const list = this.model.where(where).with(.appletVersion).select().toArray(];
        const list = array_column(list, null, 'version_num'];
        ksort(list];
        const site_version = reset(list);
        return site_version.version || '';
    }

    /**
     * 获取当前站点最新可升级的小程序版本
     * @param int site_id
     * @param string type
     * @return void
     */
    async getUpgradeVersion(int site_id, string type)
    {
        //查询下一次升级或下载的版本
        const version = this.getLastVersion(site_id, type];
        const where = [['type', '=', type]);
        if (!version) {
            const version_num = version_to_int(version];
            where[] = ['version_num', '>', version_num);
  }

  /**
   * getLastVersion
   * 对应 PHP: CoreAppletSiteVersionService_core::getLastVersion()
   * 逻辑类型: undefined - undefined
   */
  async getLastVersion(site_id: number, type: number, action: number) {
    // 基于PHP真实逻辑: getLastVersion
    // PHP原文: $where = [['site_id', '=', $site_id], ['type', '=', $type]];         $list = $this->model->where($where)->with(['appletVersion'])->select()->toArray()...
const where = [['site_id', '=', site_id], ['type', '=', type]];
        const list = this.model.where(where).with(.appletVersion).select().toArray(];
        const list = array_column(list, null, 'version_num'];
        ksort(list];
        const site_version = reset(list);
        return site_version.version || '';
    }

    /**
     * 获取当前站点最新可升级的小程序版本
     * @param int site_id
     * @param string type
     * @return void
     */
    async getUpgradeVersion(int site_id, string type)
    {
        //查询下一次升级或下载的版本
        const version = this.getLastVersion(site_id, type];
        const where = [['type', '=', type]);
        if (!version) {
            const version_num = version_to_int(version];
            where[] = ['version_num', '>', version_num);
  }

  /**
   * getUpgradeVersion
   * 对应 PHP: CoreAppletSiteVersionService_core::getUpgradeVersion()
   * 逻辑类型: undefined - undefined
   */
  async getUpgradeVersion(site_id: number, type: number) {
    // 基于PHP真实逻辑: getUpgradeVersion
    // PHP原文: //查询下一次升级或下载的版本         $version = $this->getLastVersion($site_id, $type);         $where = [['type', '=', $type]];         if (!$version) {          ...
//查询下一次升级或下载的版本
        const version = this.getLastVersion(site_id, type];
        const where = [['type', '=', type]);
        if (!version) {
            const version_num = version_to_int(version];
            where[] = ['version_num', '>', version_num);
  }
}
